<template>
  <div class="report-wrap">
    <el-form class="form">
      <el-form-item label-width="100px" label="信息类型：">
        <el-select v-model="infortype" placeholder="请选择">
          <el-option
            v-for="item in inforType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="140px" label="信息发布人手机：">
        <el-input v-model="phone" type="text"/>
      </el-form-item>
      <el-form-item label-width="120px" label="举报人手机：">
        <el-input v-model="reportphone" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="举报类型：">
        <el-select v-model="reportcode" placeholder="请选择">
          <el-option
            v-for="item in infos"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="举报时间：">
        <el-date-picker
          v-model="pickDate"
          :picker-options="pickerOptions2"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="left"/>
      </el-form-item>
      <el-form-item label-width="50px">
        <el-button type="primary" icon="el-icon-search" @click="handleSubmitSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="index" label="序号" width="180"/>
      <el-table-column prop="typename" label="举报类型" width="200"/>
      <el-table-column prop="reportcontent" label="内容描述" width="300"/>
      <el-table-column prop="reportname" label="举报人" width="200"/>
      <el-table-column prop="reportphone" label="举报人手机" width="200"/>
      <el-table-column prop="companyname" label="信息名称/企业名称" width="200"/>
      <el-table-column prop="userid" label="发布人" width="200"/>
      <el-table-column prop="phone" label="信息发布人手机" width="200"/>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="danger" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      :visible.sync="centerDialogVisible"
      title="提示"
      width="30%"
      center>
      <span>确认删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { findChild } from '@/api/data'
import { reportMessage, reportDelte } from '@/api/report'
export default {
  name: 'Publish',
  components: {
    Pagination
  },
  data() {
    return {
      reportId: 'ec14ad686e4f4a5ea88336aadc387832',
      phone: '',
      reportphone: '',
      infos: [],
      inforType: [
        {
          label: '全部',
          value: 0
        },
        {
          label: '租赁',
          value: 1
        },
        {
          label: '销售',
          value: 2
        },
        {
          label: '维修',
          value: 3
        }
      ],
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      pickDate: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      },
      centerDialogVisible: false,
      state: '',
      iDisplayStart: 0,
      iDisplayLength: 10,
      infortype: 0,
      reportcode: '',
      fromtime: '',
      totime: '',
      list: [],
      type: []
    }
  },
  computed: {
    tableData() {
      const result = []
      this.list.map((item, index) => {
        const idx = this.type.findIndex((subitem) => {
          return item.reportcode === subitem.code
        })
        console.log(idx)
        item = Object.assign({}, item, { index: `${(this.listQuery.page - 1) * this.listQuery.limit + 1 + index}`, edit: false, typename: this.type[idx].name })
        result.push(item)
      })
      return result
    }
  },
  created() {
    this.getReportMessage()
  },
  methods: {
    handleDelete(scope) {
      const id = scope.row.inforid
      reportDelte(id)
        .then((res) => {
          this.$message.success({
            message: '删除成功',
            duration: 1000
          })
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
      this.centerDialogVisible = true
    },
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.iDisplayStart = (prop.page - 1) * prop.limit
      this.iDisplayLength = prop.limit
      this.getReportMessage()
    },
    async getReportType() {
      const id = this.reportId
      const result = []
      await findChild(id)
        .then((res) => {
          const data = res.data
          this.type = data
          console.log(this.type)
          data.map((item, index) => {
            result.push({
              value: item.code,
              label: item.name
            })
          })
          this.infos = result
        })
    },
    async getReportMessage() {
      await this.getReportType()
      const { state, iDisplayStart, iDisplayLength } = this
      reportMessage(state, iDisplayStart, iDisplayLength)
        .then((res) => {
          const data = res.data
          this.list = data
          this.total = res.total
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    handleSubmitSearch() {
      let fromtime, totime
      const { state, iDisplayStart, iDisplayLength, phone, reportphone, infortype, reportcode, pickDate } = this
      if (!pickDate.length) {
        fromtime = ''
        totime = ''
      } else {
        fromtime = `${this.time(pickDate[0])} ${new Date(pickDate[0]).toTimeString().substring(0, 8)}`
        totime = `${this.time(pickDate[1])} ${new Date(pickDate[1]).toTimeString().substring(0, 8)}`
      }
      reportMessage(state, iDisplayStart, iDisplayLength, phone, reportphone, infortype, reportcode, fromtime, totime)
        .then((res) => {
          const data = res.data
          this.list = data
          this.total = res.total
          console.log(res)
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    zillZero(num) {
      if (num < 10) {
        return `0${num}`
      } else {
        return num
      }
    },
    time(date) {
      let month = new Date(date).getMonth() + 1
      month = this.zillZero(month)
      const year = new Date(date).getFullYear()
      let day = new Date(date).getDate()
      day = this.zillZero(day)
      return `${year}-${month}-${day}`
    }
  }
}
</script>
<style lang="scss" scoped>
.report-wrap {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .form {
    display: flex;
    flex-wrap: wrap;
  }
  .switch-btn {
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: 20px;
  }
}
</style>
